package com.fy.sys.controller;

import com.fy.core.enums.ApiErrorEnum;
import com.fy.core.util.JWTUtil;
import com.fy.core.vo.ApiRest;
import com.fy.sys.dto.UserPasswordDto;
import com.fy.sys.entity.User;
import com.fy.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author hxd
 * @since 2023-06-14
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    public ApiRest login(@RequestBody User user, String code, String id){
        return userService.login(user,code,id);
    }

    @GetMapping("verify")
    public ApiRest verify(@RequestHeader("token") String token){
        User user = JWTUtil.verifyToken(token);
        if (user == null){
            return ApiRest.failure(ApiErrorEnum.LOGIN_TOKEN_IS_INVALID, "");
        }else {
            return ApiRest.success();
        }
    }

    @PostMapping("updateUserProfile")
    public ApiRest updateUserProfile(@RequestBody User user){
        return userService.updateUserProfile(user);
    }

    @PostMapping("updateUserPwd")
    public ApiRest updateUserPwd(@RequestBody UserPasswordDto userPasswordDto){
        return userService.updateUserPwd(userPasswordDto);
    }

}
